package com.solr.self.search;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;

public class newsInfoSearch {

	public static void main(String[] args) {
		SolrSearchBasePO sbpo = new SolrSearchBasePO();
		sbpo.setParam("key", "工程师");
		sbpo.setParam("type", "title");
		// 要返回的字段，如果此处未设置，则返回全部字段
//		sbpo.setFl("id,title,author,content");
//		// 设置排序字段，使用的是LinkedHashMap来控制排序的
		sbpo.setOrderParam("id", SolrQuery.ORDER.desc); 
//		// 设置高亮字段
//		sbpo.setIs_hl(true);
//		sbpo.setHl_fl("title,content");

		// 把分页参数放到sbpo中
		sbpo.setOffset(0);
		sbpo.setLimit(10);
//        DataGrid<List<SolrDocument>> listDataGrid = SolrjOperator.getResultDataForJson(sbpo);
//        DataGrid<List<Map<String,Object>>> listDataGrid = SolrjOperator.getResultDataForMap(sbpo);
//        DataGrid<List<NewsBO>> listDataGrid = SolrjOperator.getResultDataForBO(sbpo, NewsBO.class);
//        System.out.println("totalRecond : "+listDataGrid.getTotalElements());
        
//        // 返回SolrDocument记录的集合
//        for (SolrDocument solrDocument : listDataGrid.getData()) {
//			System.out.println(solrDocument.toString());
//		}
        // 返回Map集合记录
//        List<Map<String,Object>> listData = listDataGrid.getData();
//        for (Map<String,Object> dataMap : listData) {
//			System.out.println(dataMap.get("id")+","+dataMap.get("title")+","+dataMap.get("author")+","+dataMap.get("content")+","+dataMap.get("type"));
//		}
        // 返回bean记录集合
//        List<NewsBO> listData = listDataGrid.getData();
//        for (NewsBO bo : listData) {
//        	System.out.println(bo.getId()+","+bo.getTitle()+","+bo.getContent()+","+bo.getAuthor()+","+bo.getType());
//		}
        
//		// 设置分组字段
		sbpo.setIs_facet(true);
		sbpo.setFacetParamStr("type");
//		sbpo.setFacetParamStr("id");
		Map<String, Map<String, Long>> facetCountMap = SolrjOperator.queryResultDataForFacet(sbpo);
		Map<String, Long> countMap = new HashMap<String, Long>();
		// 设置排序字段
		Iterator<Map.Entry<String, Map<String, Long>>> facetCountIt = facetCountMap.entrySet().iterator();
		while (facetCountIt.hasNext()) {
//			String facet_field = facetCountIt.next().getKey();
			countMap = facetCountIt.next().getValue();
			Iterator<String> keyValueIt = countMap.keySet().iterator();
			while (keyValueIt.hasNext()) {
				String key = keyValueIt.next();
				System.out.println(key+","+countMap.get(key));
			}
		}
        
	}
}
